package nick.xbmc.library
{
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	public class TVShowFactory
	{
		private var prefix:String = "http://";
		//private var server:String = "nickharbin/";
		//private var server:String = "192.168.2.4/";
		//private var server:String = "mce/";
		private var server:String = "localhost/";
		private var middle:String = "xbmcCmds/xbmcHttp?command=";
		private var queryVid:String = "queryvideodatabase(";
		private var queryArray:Array = new Array();
		private var postfix:String = ")";
		
		private var test:String;
		
		public function TVShowFactory()
		{
			initQueryArray();
			
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, buildTVShowArray);
			//loader.load(new URLRequest("http://localhost/xbmcCmds/xbmcHttp?command=queryvideodatabase%28select%20tvshow.c06,tvshow.idShow,tvshow.c00,tvshow.c01,tvshow.c04,tvshow.c05,tvshow.c08,tvshow.c11,path.strPath,files.strFilename,episode.idEpisode,episode.c00,episode.c01,episode.c03,episode.c04,episode.c05,episode.c10,episode.c12,episode.c13,episode.c06 from episode inner join tvshowlinkepisode on episode.idEpisode=tvshowlinkepisode.idEpisode inner join tvshow on tvshowlinkepisode.idShow=tvshow.idShow inner join tvshowlinkpath on tvshow.idShow=tvshowlinkpath.idShow inner join files on episode.idFile=files.idFile inner join path on files.idPath=path.idPath)"));
			loader.load(new URLRequest(prefix + server + middle + queryVid + "select " + queryArray["TVShowThumbnailURLS"] + "," +
																						 queryArray["TVShowID"] + "," +
																						 queryArray["TVShowTitle"] + "," +
																						 queryArray["TVShowSummary"] + "," +
																						 queryArray["TVShowRating"] + "," +
																						 queryArray["TVShowFirstAired"] + "," +
																						 queryArray["TVShowGenre"] + "," +
																						 queryArray["TVShowFanartURLS"] + "," +
																						 queryArray["TVShowPath"] + "," +
																						 queryArray["TVShowFile"] + "," +
																						 queryArray["EpisodeID"] + "," +
																						 queryArray["EpisodeTitle"] + "," +
																						 queryArray["EpisodePlotSummary"] + "," +
																						 queryArray["EpisodeRating"] + "," +
																						 queryArray["EpisodeWriter"] + "," +
																						 queryArray["EpisodeFirstAired"] + "," +
																						 queryArray["EpisodeDirector"] + "," +
																						 queryArray["EpisodeSeason"] + "," +
																						 queryArray["EpisodeNumber"] + "," +
																						 queryArray["EpisodeThumbnailURLS"] +
																			  " from episode inner join tvshowlinkepisode on " +
																			  			 queryArray["EpisodeID"] + "=" + queryArray["TVShowLinkEpisodeIDEpisode"] +
																			  " inner join tvshow on " +
																			  			 queryArray["TVShowLinkEpisodeIDShow"] + "=" + queryArray["TVShowID"] +
																			  " inner join tvshowlinkpath on " +
																			  			 queryArray["TVShowID"] + "=" + queryArray["TVShowLinkPathIDShow"] +
																			  " inner join files on " +
																			  			 queryArray["EpisodeIDFile"] + "=" + queryArray["TVShowFileIDFile"] +
																			  " inner join path on " +
																			  			 queryArray["TVShowFileIDPath"] + "=" + queryArray["TVShowPathIDPath"] +
																			  postfix));																			  
		}
		
		private function xmlListToArray(xmlList:XMLList, urlPrefix:String=null):Array
		{
			var returnArray:Array = new Array(xmlList.length());
			
			if (urlPrefix != null)
			{
				for (var i:Number = 0; i < returnArray.length; i++)
				{
					returnArray[i] = urlPrefix + xmlList[i];
				}
			}
			else
			{
				for (var x:Number = 0; x < returnArray.length; x++)
				{
					returnArray[x] = xmlList[x];
				}
			}
			
			return returnArray;
		}
		
		private function buildTVShowArray(event:Event):void
		{
			var xml:XML = new XML(event.target.data);
			
			for (var i:Number = 0; i < xml.field.length(); i++)
			{
				if (xml.field[i].thumb.length() != 0)
				{
					var tvshow:TVShow = new TVShow();
				}
			}
		}
		
		private function initQueryArray():void
		{
			queryArray["TVShowID"] = "tvshow.idShow";
			queryArray["TVShowTitle"] = "tvshow.c00";
			queryArray["TVShowSummary"] = "tvshow.c01";
			queryArray["TVShowRating"] = "tvshow.c04";
			queryArray["TVShowFirstAired"] = "tvshow.c05";
			queryArray["TVShowThumbnailURLS"] = "tvshow.c06";
			queryArray["TVShowGenre"] = "tvshow.c08";
			queryArray["TVShowFanartURLS"] = "tvshow.c11";
			queryArray["TVShowLinkEpisodeIDEpisode"] = "tvshowlinkepisode.idEpisode";
			queryArray["TVShowLinkEpisodeIDShow"] = "tvshowlinkepisode.idShow";
			queryArray["TVShowLinkPathIDShow"] = "tvshowlinkpath.idShow";
			queryArray["TVShowLinkPathIDPath"] = "tvshowlinkpath.idPath";
			queryArray["TVShowPathIDPath"] = "path.idPath";
			queryArray["TVShowPath"] = "path.strPath";
			queryArray["TVShowFileIDFile"] = "files.idFile";
			queryArray["TVShowFileIDPath"] = "files.idPath";
			queryArray["TVShowFile"] = "files.strFilename";
			queryArray["EpisodeID"] = "episode.idEpisode";
			queryArray["EpisodeTitle"] = "episode.c00";
			queryArray["EpisodePlotSummary"] = "episode.c01";
			queryArray["EpisodeRating"] = "episode.c03";
			queryArray["EpisodeWriter"] = "episode.c04";
			queryArray["EpisodeFirstAired"] = "episode.c05";
			queryArray["EpisodeThumbnailURLS"] = "episode.c06";
			queryArray["EpisodeDirector"] = "episode.c10";
			queryArray["EpisodeSeason"] = "episode.c12";
			queryArray["EpisodeNumber"] = "episode.c13";
			queryArray["EpisodeIDFile"] = "episode.idFile";
		}
		
		//Placeholder for a working sql query. Not every field is returned yet though.
		//select tvshow.c00,episode.c00,episode.c01,path.strPath,files.strFilename from episode inner join tvshowlinkepisode on episode.idEpisode = tvshowlinkepisode.idEpisode inner join tvshow on tvshowlinkepisode.idShow = tvshow.idShow inner join tvshowlinkpath on tvshow.idShow = tvshowlinkpath.idShow inner join files on episode.idFile = files.idFile inner join path on files.idPath = path.idPath
	}
}